/*
* coder: Dzlua
* email: 505544956@qq.com
* time : 2018-04-13 16:25
*/

#include "ui/ui.hpp"

#include <iostream>

class MyCls
{
public:
    MyCls(int i) : m_n(i) {}
    static auto instance(int i) {
        return std::make_shared<MyCls>(i);
    }
public:
    int get() { return m_n; }
protected:
    int m_n;
};

void show(ui::CArray<std::shared_ptr<MyCls>> &array) {
    std::cout << "-------show---------" << std::endl;
    std::cout << "show array:" << array.size() << std::endl;
    for (auto itor = array.begin(); itor != array.end(); ++itor) {
        std::cout << (*itor)->get() << ",";
    }
    std::cout << std::endl;
    std::cout << "----------------" << std::endl;
}

void show1(ui::CArray<std::shared_ptr<MyCls>> &array) {
    std::cout << "--------show1--------" << std::endl;
    std::cout << "show array:" << array.size() << std::endl;
    for (auto itor = array.rbegin(); itor != array.rend(); ++itor) {
        std::cout << (*itor)->get() << ",";
    }
    std::cout << std::endl;
    std::cout << "----------------" << std::endl;
}

void show2(ui::CArray<std::shared_ptr<MyCls>> &array) {
    std::cout << "--------show2--------" << std::endl;
    std::cout << "show array:" << array.size() << std::endl;
    for (int i = 0; i < array.size(); ++i) {
        auto ele = array.at(i);
        std::cout << ele->get() << ",";
    }
    std::cout << std::endl;
    std::cout << "----------------" << std::endl;
}

void test_add_insert() {
    ui::CArray<std::shared_ptr<MyCls>> array;
    show(array);
    for (int i = 0; i <= 10; ++i) {
        auto ele = MyCls::instance(i);
        array.add(ele);
    }
    show(array);
    array.insert(MyCls::instance(11), 9);
    show(array);
    array.insert(MyCls::instance(12), 8);
    show(array);
    // insert at first
    array.insert(MyCls::instance(13), 0);
    show(array);
    // insert at 
    array.insert(MyCls::instance(14), 14);
    show(array);

    array.clear();
    for (int i = 0; i <= 10; ++i) {
        auto ele = MyCls::instance(i);
        array.add(ele);
    }
    show(array);
    array.insert(MyCls::instance(11), array.end());
    show(array);
    array.insert(MyCls::instance(12), array.end() - 1);
    show(array);
    array.insert(MyCls::instance(13), array.begin() );
    show(array);
    array.insert(MyCls::instance(14), array.begin() + 1 );
    show(array);
}

void test_erase() {
    ui::CArray<std::shared_ptr<MyCls>> array;
    for (int i = 0; i <= 10; ++i) {
        auto ele = MyCls::instance(i);
        array.add(ele);
    }
    show(array);
    // ele
    auto first = array.front();
    auto back = array.back();
    array.erase(first);
    array.erase(back);
    show(array);
    // idx
    array.erase(0);
    array.erase(array.size() - 1);
    show(array);
    // iterator
    array.erase(array.begin());
    show(array);
    array.erase(array.end() - 1);
    show(array);
}

void test_base() {
    ui::CArray<std::shared_ptr<MyCls>> array;
    for (int i = 0; i <= 10; ++i) {
        auto ele = MyCls::instance(i);
        array.add(ele);
    }
    show(array);
    show1(array);
    show2(array);
    std::cout << "----------------" << std::endl;
    std::cout << "index, iterator" << std::endl;
    for (int i = 0; i < array.size(); ++i) {
        auto ele = array.at(i);

        size_t idx;
        if (array.index(ele, &idx)) std::cout << idx << ",";

        ui::CArray<std::shared_ptr<MyCls>>::TMAP_ITOR itor;
        if (array.iterator(ele, &itor)) std::cout << (*itor)->get() << "|";
    }
    std::cout << std::endl;
    std::cout << "----------------" << std::endl;
    std::cout << array.empty() << std::endl;
    array.clear();
    std::cout << array.empty() << std::endl;
    show(array);
}

int main(int argc, char** argv)
{
    test_add_insert();
    std::cout << "------test_erase----------" << std::endl;
    test_erase();
    std::cout << "------test_base----------" << std::endl;
    test_base();
    return 0;
}